package com.aiatomic.couting.password;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.aiatomic.couting.ICodeMsg;
import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.PasswordForgetActivity;
import com.aiatomic.couting.R;
import com.aiatomic.couting.RegisterActivity;
import com.aiatomic.couting.util.HttpUtils;
import com.aiatomic.couting.util.ImageUtils;
import com.aiatomic.couting.util.SystemUtil;
import com.aiatomic.couting.util.ValidatorUtils;
import com.aiatomic.couting.widgets.LineEditText;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentForgetPassword1 extends Fragment {
	private View view;
	private LineEditText account_forget_pass_et;
	private LineEditText forget_pass_valide_code;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_password_forget1, container, false);
		((ImageView) view.findViewById(R.id.forget_pass_valide_image))
				.setImageBitmap(ImageUtils.getInstance().createBitmap());
		account_forget_pass_et=(LineEditText) view.findViewById(R.id.account_forget_pass_et);
		forget_pass_valide_code=(LineEditText) view.findViewById(R.id.forget_pass_valide_code);
		view.findViewById(R.id.forget_pass_valide_image).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ImageView iv = (ImageView) v;
				iv.setImageBitmap(ImageUtils.getInstance().createBitmap());
			}
		});

		TextView txtBack = (TextView) view.findViewById(R.id.txtBack);
		txtBack.setText(R.string.back);
		txtBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				getActivity().onBackPressed();
			}
		});
		TextView txtNext = (TextView) view.findViewById(R.id.txtNext);
		txtNext.setVisibility(View.INVISIBLE);

		TextView txtMiddle = (TextView) view.findViewById(R.id.txtMiddle);
		txtMiddle.setText(R.string.get_forget_pass);
		init();
		return view;
	}

	private void init() {
		view.findViewById(R.id.btn_next).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(!forget_pass_valide_code.getText().toString().toLowerCase().equals(ImageUtils.getInstance().getCode().toLowerCase())){
					forget_pass_valide_code.setError(getResources().getText(R.string.err_input_valid_code));
					forget_pass_valide_code.setFocusable(true);
					((ImageView) view.findViewById(R.id.forget_pass_valide_image))
					.setImageBitmap(ImageUtils.getInstance().createBitmap());
					return;
				}
				if (!ValidatorUtils.isMobileNO(account_forget_pass_et.getText().toString())) {
					account_forget_pass_et.setError(getResources().getString(R.string.err_input_phone));
					account_forget_pass_et.setFocusable(true);
					return;
				}
				s_account_v();
			
			}
		});
	}

	
	private void s_account_v(){
		Map<String, String> params = new HashMap<String, String>();
		params.put("mobile", account_forget_pass_et.getText().toString());
		params.put("platform", String.valueOf(IConstant.PLATFORM));
		params.put("channel", String.valueOf(IConstant.CHANNEL_MOB));
		params.put("sign",
				HttpUtils.md5( params.get("mobile")+ params.get("platform") + params.get("channel")
						+ IConstant.NORMAL_MD5_KEY));
		HttpUtils.post(new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if (msg.what == HttpUtils.CODE_SUCCESS_RQ_100) {
					try {
						JSONObject json = new JSONObject(msg.obj.toString());
						
						if (json.getInt("code") == HttpUtils.RET_SUCCESS) {
							FragmentForgetPassword2 fr = new FragmentForgetPassword2(json.getString("aid"),json.getString("mobile"));
						
							PasswordForgetActivity passwordForgetActivity = (PasswordForgetActivity) FragmentForgetPassword1.this.getActivity();
							passwordForgetActivity.addFragment(fr);
						}else{
							Toast toast = Toast.makeText(getActivity(), json.getString("msg"),
									Toast.LENGTH_LONG);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
						}
					} catch (JSONException e) {
						e.printStackTrace();
						Log.e("", msg.obj.toString());
						Toast toast = Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.server_format_error), Toast.LENGTH_LONG);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}
				} else if (msg.what == HttpUtils.CODE_ERROR_RQ_999) {
					Toast toast = Toast.makeText(getActivity(),
							ICodeMsg.ERROR_CODES_HTTP.get(msg.what), Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
			}

		}, IConstant.CENTER + "/v/Account_c/account_v/", params);
	}
}
